package com.witte.controller;

import com.witte.entity.*;
import com.witte.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Controller
@ResponseBody
@RequestMapping("/api/Sales")
@CrossOrigin
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping(value = "/findAll")
    public RespBean begin() {
        //System.out.println(productService.selectAll());
        RespBean respBean =RespBean.ok(null, productService.selectAll());
        return respBean ;
    }
    @PostMapping(value = "/deleteProduct")
    public RespBean delete(@RequestBody Product product) {
        RespBean respBean =RespBean.ok(null, productService.deleteByPrimaryKey(product.getProductno()));
        return respBean ;
    }

    @PostMapping(value = "/updateProduct")
    public RespBean save(@RequestBody Product product) {
        RespBean respBean =RespBean.ok(null, productService.updateByProductNo(product));
        return respBean ;
    }
    @PostMapping(value = "/insertProduct")
    public RespBean insert(@RequestBody Product product) {
        String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<10;i++){
            int number=random.nextInt(36);
            sb.append(str.charAt(number));
        }
        product.setProductno(sb.toString());
        RespBean respBean =RespBean.ok(null, productService.insertSelective(product));
        return respBean;
    }
    @GetMapping(value="/getStateList")
    public RespBean getStateList(Integer beginPage,Integer pageSize){
       // System.out.println("111");
        List<Product> list=productService.getListForPage(beginPage,pageSize);
        RespBean respBean=RespBean.error("新加错误！");
        if(list!=null) {
            respBean=RespBean.ok("ok",list);
            respBean.setTotleSize(productService.getRecodeCount());
        }
        return respBean;
    }
    @GetMapping(value="/getNameList")
    public RespBean getNameList(String name ,Integer beginPage,Integer pageSize){
         //System.out.println("111");
        List<Product> list=productService.getListByProductName(name,beginPage,pageSize);
        RespBean respBean=RespBean.error("新加错误！");
        if(list!=null) {
            respBean=RespBean.ok("ok",list);
            respBean.setTotleSize(productService.getNameCount(name));

        }
        //System.out.println(respBean.totleSize);
        return respBean;
    }
/*
    @GetMapping("/searchName")
    public RespBean getNameList(String name,Integer beginPage,Integer pageSize){
        List<Customer> list=cusService.getNameList(name,beginPage,pageSize);
        RespBean respBean=RespBean.error("获取错误！");
        if(list!=null) {
            respBean=RespBean.ok("ok",list);
            respBean.setTotleSize(cusService.getNameCount(name));
        }
        //System.out.println(beginPage + ',' + pageSize);
        /// System.out.println(cusService.getRecodeCount());
        return respBean;
    }
*/

}
